package com.example.demo.mapper;


import com.example.demo.entity.Order;
import org.apache.ibatis.annotations.*;
import org.springframework.data.repository.query.Param;

import java.util.List;

@Mapper
public interface OrderMapper {
    @Insert("INSERT INTO orders (user_id, service_id, order_no," +
            " amount,duration, status, create_time, pay_time,update_time) " +
            "VALUES (#{userId}, #{serviceId}, #{orderNo}," +
            " #{amount},#{duration}, #{status}, #{createTime}, #{payTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertOrder(Order order);

    @Update("UPDATE orders SET status = #{status}, " +
            "pay_time = #{payTime} WHERE order_no = #{orderNo}")
    void updateOrderStatusByOrderNo(@Param("orderNo") String orderNo,
                           @Param("status") int status,
                           @Param("payTime") String payTime);
    @Update("UPDATE orders SET status = #{status}, " +
            "update_time = #{updateTime} WHERE order_no = #{orderNo}")
    void refundOrderStatusByOrderNo(@Param("orderNo") String orderNo,
                                    @Param("status") int status,
                                    @Param("payTime") String payTime);
    @Update("UPDATE orders SET status = #{status}, " +
            "update_time = #{updateTime} WHERE id = #{id}")
    int updateOrderStatus(@Param("id") int id, @Param("status") int status,
                          @Param("updateTime") String updateTime);

    @Select("SELECT status FROM orders WHERE order_no = #{order_no}")
    int getStatusByOrderNo(String orderNo);

    @Select("SELECT pay_time FROM orders WHERE order_no = #{order_no}")
    String getPayTimeByOrderNo(String orderNo);

    @Select("SELECT * FROM orders WHERE order_no = #{order_no}")
    Order getOrderByOrderNo(String order_no);


    // 统计方法
    @Select("SELECT COUNT(*) FROM orders WHERE user_id = #{userId} AND status = #{status}")
    int countByStatus(@Param("userId") int userId, @Param("status") int status);

    // 订单列表查询
    @Select("SELECT o.*, s.serviceName, s.serviceImage AS serviceName, serviceImage " +
            "FROM orders o " +
            "LEFT JOIN voiceservice s ON o.service_id = s.id " +
            "WHERE o.user_id = #{userId} AND o.status = #{status} " +
            "AND o.amount >= #{minAmount} AND o.amount <= #{maxAmount} " +
            "AND o.create_time >= #{startTime} AND o.create_time <= #{endTime} " +
            "ORDER BY o.id DESC " +
            "LIMIT #{offset}, #{limit}")
    List<Order> selectOrderList(
        @Param("userId") int userId,
        @Param("status") Integer status,
        @Param("minAmount") Double minAmount,
        @Param("maxAmount") Double maxAmount,
        @Param("startTime") String startTime,
        @Param("endTime") String endTime,
        @Param("offset") int offset,
        @Param("limit") int limit);

    // 订单列表查询
    @Select("SELECT o.*, s.serviceName, s.serviceImage AS serviceName, serviceImage " +
            "FROM orders o " +
            "LEFT JOIN voiceservice s ON o.service_id = s.id " +
            "WHERE o.user_id = #{userId} AND o.status = #{status} " +
            "ORDER BY o.create_time DESC " +
            "LIMIT #{offset}, #{limit}")
    List<Order> selectOrders(
            @Param("userId") int userId,
            @Param("status") Integer status,
            @Param("offset") int offset,
            @Param("limit") int limit);


    // 订单详情查询
    @Select("SELECT o.*, s.serviceName, s.serviceImage AS serviceName, serviceImage " +
            "FROM orders o " +
            "LEFT JOIN voiceservice s ON o.service_id = s.id " +
            "WHERE o.id = #{id}")
    Order selectById(int id);

    @Select("SELECT o.*, s.serviceName AS serviceName, serviceImage " +
            "FROM orders o " +
            "LEFT JOIN voiceservice s ON o.service_id = s.id " +
            "WHERE o.user_id = #{userId} " +
            "ORDER BY o.create_time DESC ")
    List<Order> selectAllOrders(int userId);


    @Select("SELECT o.*, s.serviceName, s.serviceImage AS serviceName, serviceImage " +  // 修复1：添加逗号分隔符
            "FROM orders o " +
            "LEFT JOIN voiceservice s ON o.service_id = s.id " +
            "WHERE o.user_id = #{userId} AND s.serviceName LIKE CONCAT('%', #{serviceName}, '%')") 
    List<Order> searchByServiceName(int userId,  String serviceName);
}
